﻿using CREAPA.Dominio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CREAPA.RepositorioADO
{
    public class RequisicaoComprasRepositorioADO : IRequisicaoCompras
    {
        public static RequisicaoComprasRepositorioADO Instance = new RequisicaoComprasRepositorioADO();

        private readonly List<RequisicaoComprasItens> _itens = new List<RequisicaoComprasItens>
                                                 {
                                                     new RequisicaoComprasItens{Id = 1, Descricao="Stuart Leeks", Quantidade=10},
                                                     new RequisicaoComprasItens{Id = 2, Descricao="Simon Ince", Quantidade=20},
                                                     new RequisicaoComprasItens{Id = 3, Descricao="UK Premier Support for Developers",Quantidade=30},
                                                 };

        protected RequisicaoComprasRepositorioADO()
        {

        }

        public IEnumerable<RequisicaoComprasItens> GetAllItens()
        {
            return _itens;
        }

        public RequisicaoComprasItens GetItem(int id)
        {
            return _itens.FirstOrDefault(b => b.Id == id);
        }

        public IEnumerable<RequisicaoComprasItens> Delete(RequisicaoComprasItens item)
        {
            _itens.RemoveAll(l => l.Id == item.Id);
            return _itens;
        }

        public void Save(RequisicaoComprasItens item)
        {
            if (item.Id <= 0)
            {
                int max = _itens.Max(b => (int?)b.Id) ?? 0;
                item.Id = max + 1;
                _itens.Add(item);
            }
            else
            {
                _itens.RemoveAll(l => l.Id == item.Id);
                _itens.Add(item);
            }
        }
        
    }
}
